// pages/orderDetails/orderDetails.js
var common = require("../../utils/Conmon.js");
Page({

  /**
   * 页面的初始数据
   */
  data: {
    UnionStoreName: "官方自营",
    StoreType: "自营",
    shopID: 0,
    OrderInfo: null,
    PickedUpCodeImg: "",
    latitude: 0,
    longitude: 0,
  },

  //保存二维码到相册
  saveImg: function () {
    var that = this;
    wx.previewImage({
      urls: [that.data.PickedUpCodeImg],
      success: res => {
        wx.showToast({
          title: '请长按图片保存.. ',
          duration: 5000
        })
      },
      fail: res => {

      }
    });
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    common.pageInit(function () { });
    wx.showLoading({
      title: '加载中...',
      mask: true
    });

    var that = this;
    that.setData({
      shopID: options.shopID
    });
    var globalData = getApp().globalData;
    var custId = globalData.custID;
    var OrderId = options.OrderID;
    var url = globalData.apiurl + '/order/api/OrderInfo/GetOrderByOrderId';

    wx.request({
      url: url,
      data: {
        "custId": custId,
        "orderId": OrderId,
        "unionShopID": that.data.shopID
      },
      method: 'GET',
      header: {
        'content-type': 'application/json',
        'X_ManuName': globalData.manuName
      },
      success: function (res) {
        var OrderInfo = res.data.content;
        getHtml(OrderInfo.GetOrderDetails);
        if (OrderInfo.SelfExtrac != null) {
          wx.getLocation({
            type: 'gcj02',
            success: function (res) {
              that.setData({
                latitude: res.latitude,
                longitude: res.longitude,
              })
            }
          });
          var distance = getDistance(that.data.latitude, that.data.longitude, OrderInfo.SelfExtrac.Latitude, OrderInfo.SelfExtrac.Longitude);
          distance = (distance / 1000).toFixed(2);
          OrderInfo.SelfExtrac.distance = distance;
        }
        that.setData({
          UnionStoreName: OrderInfo.UnionStoreID == 0 ? "官方自营" : OrderInfo.UnionShopName,
          StoreType: OrderInfo.UnionStoreID == 0 ? "自营" : "商家",
          OrderInfo: OrderInfo,
        });

        if (OrderInfo != null && OrderInfo.OrderPickedUp != null && OrderInfo.OrderPickedUp.PickedUpCode != "") {
          var link = "/pages/OrderPickedUp/OrderPickedUp?code=" + OrderInfo.OrderPickedUp.PickedUpCode + "&unionShopID=" + that.data.shopID;

          wx.request({
            url: globalData.apiurl + '/mobile/api/OrderInfo/GetPickedUpOrderCode',
            data: {
              "pageLink": link,
              "ZpAppId": globalData.zhengpinAppid,
              "smallAppId": globalData.zpAppid,
              "smallSecret": globalData.secret,
              "isAuthComponent": globalData.isAuthComponent
            },
            method: 'GET',
            header: {
              'content-type': 'application/json',
              'X_ManuName': globalData.manuName
            },
            success: function (res) {
              if (res.data.code == 1200) {
                that.setData({
                  PickedUpCodeImg: "data:image/png;base64," + res.data.content,
                })
              }
            }
          })
        }

        wx.hideLoading();
      },
      fail: function () {
        wx.hideLoading();
        wx.showToast({
          title: '获取订单详情失败',
          duration: 2000
        })
      },
      complete: function () {
        //complete
      }
    })
  },

  getmap: function (e) {
    var OrderInfo = this.data.OrderInfo;
    wx.openLocation({
      latitude: parseFloat(OrderInfo.SelfExtrac.Latitude),
      longitude: parseFloat(OrderInfo.SelfExtrac.Longitude),
      name: OrderInfo.SelfExtrac.ShopName,
      scale: 28
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var that = this;
    wx.getLocation({
      type: 'gcj02',
      success: function (res) {
        that.setData({
          latitude: res.latitude,
          longitude: res.longitude,
        })
      }
    });
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})

function getHtml(d) {
  if (d.length > 0) {
    for (var j = 0; j < d.length; j++) {
      if (d[j].acitivtys != null && d[j].acitivtys != "") {
        var ac = d[j].acitivtys.split(",");
        if (ac.length > 0) {
          d[j].acitivtys = "";
          for (var k = 0; k < ac.length; k++) {
            switch (ac[k]) {
              case "1":
                d[j].acitivtys += '<span class="yute zu">组</span>';
                break;
              case "2":
                d[j].acitivtys += '<span class="yute man">兑</span>';
                break;
              case "3":
                d[j].acitivtys += '<span class="yute xian">限</span>';
                break;
              case "4":
                d[j].acitivtys += '<span class="yute man">满</span>';
                break;
              case "5":
                d[j].acitivtys += '<span class="yute man">复</span>';
                break;
              case "6":
                d[j].acitivtys += '<span class="yute miao">返</span>';
                break;
              case "7":
                d[j].acitivtys += '<span class="yute tuan">拼</span>';
                break;
              case "8":
                d[j].acitivtys += '<span class="yute miao">秒</span>';
                break;
            }
          }
        }
      } else {
        d[j].acitivtys = "";
      }
    }
  }
}

//计算两点之间的距离
function getDistance(lat1, lng1, lat2, lng2) {
  lat1 = lat1 || 0;
  lng1 = lng1 || 0;
  lat2 = lat2 || 0;
  lng2 = lng2 || 0;

  var rad1 = lat1 * Math.PI / 180.0;
  var rad2 = lat2 * Math.PI / 180.0;
  var a = rad1 - rad2;
  var b = lng1 * Math.PI / 180.0 - lng2 * Math.PI / 180.0;

  var r = 6378137;
  var distance = r * 2 * Math.asin(Math.sqrt(Math.pow(Math.sin(a / 2), 2) + Math.cos(rad1) * Math.cos(rad2) * Math.pow(Math.sin(b / 2), 2)));

  return distance;
}